<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="渠道名称" prop="tradeQudao">
        <el-input
          v-model="queryParams.tradeQudao"
          placeholder="请输入渠道名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="交易结果" prop="tradeResult">
        <el-select v-model="queryParams.tradeResult" placeholder="请选择交易涉及用户类型" clearable size="small">
          <el-option v-for="dict in tradeResult" :label="dict.dictLabel" :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="对账批次" prop="batchNo">
        <el-input
          v-model="queryParams.batchNo"
          placeholder="请输入对账批次"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="交易金额" prop="amount">
        <el-input
          v-model="queryParams.amount"
          placeholder="请输入交易金额"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item><br/>
      <el-form-item label="是否差错" prop="hasDiff">
        <el-select v-model="queryParams.hasDiff" placeholder="请选择是否差错" clearable size="small">
          <el-option label="否" value="0" />
          <el-option label="是" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="差错处理结果" prop="processState">
        <el-select v-model="queryParams.processState" placeholder="请选择是否差错" clearable size="small">
          <el-option label="未处理" value="1" />
          <el-option label="要结算" value="2" />
          <el-option label="不结算" value="3" />
          <el-option label="已处理" value="4" />
        </el-select>
      </el-form-item>
      <el-form-item label="支付方式" prop="payWay">
        <el-select v-model="queryParams.payWay" placeholder="请选择交易涉及用户类型" clearable size="small">
          <el-option v-for="dict in payWay" :label="dict.dictLabel" :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="detailList">
      <el-table-column label="对账批次" align="center" prop="batchNo" />
      <el-table-column label="交易流水号" align="center" prop="payOrderNo" />
      <el-table-column label="渠道订单号" align="center" prop="qudaoOrderNo" />
      <el-table-column label="渠道名称" align="center" prop="tradeQudao" />
      <el-table-column label="交易结果" align="center" prop="tradeResult">
        <template slot-scope="scope">
          <span v-if="scope.row.tradeResult==1">处理中</span>
          <span v-else-if="scope.row.tradeResult==2">成功</span>
          <span v-else>失败</span>
        </template>
      </el-table-column>
      <el-table-column label="交易金额" align="center" prop="amount" />
      <el-table-column label="交易日期" align="center" prop="tradeDateZf" />
      <el-table-column label="手续费" align="center" prop="commissionFee" />
      <el-table-column label="支付方式" align="center" prop="payWay">
        <template slot-scope="scope">
          <span v-if="scope.row.payWay==1">微信扫码</span>
          <span v-else-if="scope.row.payWay==2">支付宝扫码</span>
          <span v-else-if="scope.row.payWay==3">微信公众号</span>
          <span v-else-if="scope.row.payWay==4">认证支付</span>
          <span v-else-if="scope.row.payWay==5">实时代付到银行卡</span>
          <span v-else>批量代付到银行卡</span>
        </template>
      </el-table-column>
      <el-table-column label="交易账户" align="center" prop="tradeAccount" />
      <el-table-column label="交易账户类型" align="center" prop="tradeAccountType">
        <template slot-scope="scope">
          <span v-if="scope.row.tradeAccountType==1">微信</span>
          <span v-else-if="scope.row.tradeAccountType==2">支付宝</span>
          <span v-else-if="scope.row.tradeAccountType==3">银行卡</span>
          <span v-else>拉卡拉</span>
        </template>
      </el-table-column>
      <el-table-column label="对账完成时间" align="center" prop="checkEndDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.checkEndDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否差错" align="center" prop="hasDiff">
        <template slot-scope="scope">
          <span v-if="scope.row.hasDiff==1">是</span>
          <span v-else>否</span>
        </template>
      </el-table-column>
      <el-table-column label="差错处理结果" align="center" prop="processState">
        <template slot-scope="scope">
          <span v-if="scope.row.processState==1">未处理</span>
          <span v-else-if="scope.row.processState==2">要结算</span>
          <span v-else-if="scope.row.processState==3">不结算</span>
          <span v-else>已处理</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.hasDiff==1 && scope.row.processState==1"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="mistakeDispose(scope.row)"
            v-hasPermi="['system:detail:edit']"
          >差错处理</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog
      title="请确认"
      :visible.sync="dialogVisible"
      width="20%">
      <span>是否进行差错处理</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="toWhetherCalculate">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="请确认"
      :visible.sync="dialogVisible2"
      width="20%">
      <span>是否需要结算</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="whetherCalculate(3)">取 消</el-button>
        <el-button type="primary" @click="whetherCalculate(2)">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {listDetail, getDetail, delDetail, addDetail, updateDetail, updateStatus} from "@/api/financing/batchDetail";

export default {
  name: "Detail",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 对账批次明细（保存对账结果明细的）表格数据
      detailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        id:null,
        pageNum: 1,
        pageSize: 10,
        billDay: null,
        checkDay: null,
        batchId: null,
        batchNo: null,
        payOrderNo: null,
        qudaoOrderNo: null,
        tradeQudao: null,
        certificateJz: null,
        certificateDz: null,
        tradeResult: null,
        amount: null,
        tradeDateZf: null,
        commissionFee: null,
        payWay: null,
        tradeAccount: null,
        tradeAccountType: null,
        checkEndDate: null,
        hasDiff: null,
        processState: null,
        completeState: null,
        confirmDate: null,
        canClearingAmount: null,
        remarks: null,
        reserve1: null,
        reserve2: null,
        reserve3: null,
        reserve4: null,
        reserve5: null,
        delFlg: null,
        createUserid: null,
        updateUserid: null,
      },
      // 表单参数
      form: {},
      tradeResult:[],
      payWay:[],
      dialogVisible:false,
      dialogVisible2:false,

    };
  },
  created() {
    this.getList();
    this.getDicts("financing_trade_result").then(response => {
      this.tradeResult = response.data;
    });
    this.getDicts("financing_pay_way").then(response => {
      this.payWay = response.data;
    });
  },
  methods: {
    /** 查询对账批次明细（保存对账结果明细的）列表 */
    getList() {
      this.loading = true;
      listDetail(this.queryParams).then(response => {
        this.detailList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 差错处理 */
    mistakeDispose(row) {
      this.dialogVisible=true;
      this.queryParams.id=row.id;
      this.queryParams.canClearingAmount=row.canClearingAmount;
      // console.log(this.queryParams.id);
      // console.log(this.queryParams.canClearingAmount);
    },
    /*是否结算弹框*/
    toWhetherCalculate() {
      if (this.queryParams.canClearingAmount != null) {
        this.dialogVisible2=true;
        this.dialogVisible=false;
        return;
      }
      this.whetherCalculate(4);
      this.dialogVisible=false;
    },
    /*是否结算*/
    whetherCalculate(val) {
      this.queryParams.processState=val;
      this.dialogVisible2=false;
      console.log(this.queryParams.processState);
      updateStatus(this.queryParams).then(response=>{
        if (response.data) {
          this.$message.success("差错处理成功");
          this.queryParams.processState=null;
          this.getList();
        }else {
          this.$message.success("差错处理失败");
        }
      })
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        billDay: null,
        checkDay: null,
        batchId: null,
        batchNo: null,
        payOrderNo: null,
        qudaoOrderNo: null,
        tradeQudao: null,
        certificateJz: null,
        certificateDz: null,
        tradeResult: null,
        amount: null,
        tradeDateZf: null,
        commissionFee: null,
        payWay: null,
        tradeAccount: null,
        tradeAccountType: null,
        checkEndDate: null,
        hasDiff: null,
        processState: null,
        completeState: null,
        confirmDate: null,
        canClearingAmount: null,
        remarks: null,
        reserve1: null,
        reserve2: null,
        reserve3: null,
        reserve4: null,
        reserve5: null,
        delFlg: null,
        createUserid: null,
        createTime: null,
        updateUserid: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加对账批次明细（保存对账结果明细的）";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDetail(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDetail(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除对账批次明细（保存对账结果明细的）编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delDetail(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/detail/export', {
        ...this.queryParams
      }, `system_detail.xlsx`)
    }
  }
};
</script>
